package com.junzhiit.nengji.cms.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import com.bainaeco.framework.util.JsonUtil;
import com.junzhiit.nengji.bo.PrinterBO;
import com.junzhiit.nengji.cms.service.PrinterService;
import com.junzhiit.nengji.cms.web.exception.PrinterIsExistException;
import com.junzhiit.nengji.cms.web.exception.PrinterNotFoundException;
import com.junzhiit.nengji.cms.web.exception.ShopNotFoundException;
import com.junzhiit.nengji.cms.web.util.Constants;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.util.erpt.PrintMessageUtils;
import com.junzhiit.nengji.cms.web.util.erpt.bean.ResultError;
import com.junzhiit.nengji.cms.web.util.erpt.bean.enums.ResponseType;
import com.junzhiit.nengji.cms.web.util.erpt.bean.enums.ResultErrorStatus;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.PrinterAddVO;
import com.junzhiit.nengji.cms.web.vo.PrinterEditVO;
import com.junzhiit.nengji.cms.web.vo.PrinterVO;
import com.junzhiit.nengji.cms.web.vo.PrinterVoiceVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.dao.PrinterDAO;
import com.junzhiit.nengji.dao.PrinterTemplateDAO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.Printer;
import com.junzhiit.nengji.model.PrinterTemplate;
import com.junzhiit.nengji.model.Shop;
import com.junzhiit.nengji.model.enums.Is;

@Service
public class PrinterServiceImpl extends BaseServiceImpl implements PrinterService{
	
	@Autowired
	private PrinterDAO printerDAO;
	
	@Autowired
	private PrinterTemplateDAO printerTemplateDAO;
	
	@Autowired
	private SystemUserCacheDAO systemUserCacheDAO;

	@Autowired
	private ShopDAO shopDAO;
	
	@Override
	public Map<String, Object> findPageAll(PrinterVO printerVO,DataTablesParamVO dtp){
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(printerVO.getSessionId()), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		
		Printer printer=new Printer();
		BeanUtils.copyProperties(printerVO, printer);
		printer.setShopId(shop.getId());
		
		List<PrinterBO> list = printerDAO.findPageAll(printer,dtp.getStart(), dtp.getLength());
		
		for(PrinterBO printerBO:list){
			int state=-1;
			
			ResultError resultError = PrintMessageUtils.getPrintStatus(printerBO.getDeviceNumber());
			if(resultError.getError()==ResultErrorStatus.SUCCESS.toIntStatus()){
				Map<String, Object> map=JsonUtil.toMap(resultError.getBody());
				state=Integer.valueOf(map.get("state").toString());
			}
			
			printerBO.setStatus(state);
		}
		
		int totalCount = printerDAO.getTotalCount(printer);
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}
	
	@Override
	public ResultVO findById(String id){
		ResultVO result=new ResultVO();
		Printer data=printerDAO.query(id);
		if(data==null){
			throw new PrinterNotFoundException("打印机未找到");
		}
		
		result.setData(data);
		return result;
	}
	
	@Override
	public ResultVO add(PrinterAddVO printerAddVO){
		ResultVO result=new ResultVO();
		
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(printerAddVO.getSessionId()), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		
		Printer printer=new Printer();
		BeanUtils.copyProperties(printerAddVO, printer);
		printer.setShopId(shop.getId());
		
		int count=printerDAO.getCount(printer);
		if(count>0){
			throw new PrinterIsExistException("打印机类型已存在");
		}
	
		//授权打印机
		PrintMessageUtils.addPrinterRequest(printerAddVO.getDeviceNumber(),printerAddVO.getDeviceKey(), null, printerAddVO.getName());
		Printer data=printerDAO.insert(printer);
		result.setData(data);
		
		return result;
	}
	
	@Override
	public ResultVO edit(String id,PrinterEditVO printerEditVO){
		ResultVO result=new ResultVO();
		
		
		Printer data=printerDAO.query(id);
		if(data==null){
			throw new PrinterNotFoundException("打印机未找到");
		}
		
		PrintMessageUtils.addPrinterRequest(printerEditVO.getDeviceNumber(),printerEditVO.getDeviceKey(), null, printerEditVO.getName());
		
		Printer printer=new Printer();
		BeanUtils.copyProperties(printerEditVO, printer);
		printer.setId(id);
		
		boolean flag=printerDAO.update(printer);
		result.setData(flag);
		
		return result;
	}
	
	@Override
	public ResultVO print(PrinterAddVO printerAddVO){
		ResultVO result=new ResultVO();
		PrintMessageUtils.addPrinterRequest(printerAddVO.getDeviceNumber(),printerAddVO.getDeviceKey(), null, printerAddVO.getName());
		PrinterTemplate printerTemplate=printerTemplateDAO.query(printerAddVO.getPrinterTemplateId());
		PrintMessageUtils.sendPrintRequest(printerAddVO.getDeviceNumber(),printerTemplate.getContent(), PrintMessageUtils.getUUID());
		return result;
	}
	
	@Override
	public ResultVO updateToDelete(String id){
		ResultVO result=new ResultVO();
		
		Printer data=printerDAO.query(id);
		if(data==null){
			throw new PrinterNotFoundException("打印机未找到");
		}
		
		boolean flag=printerDAO.updateToDelete(id);
		result.setData(flag);
		
		return result;
	}
	
	@Override
	public ResultVO shutdown(String id){
		ResultVO result=new ResultVO();
		
		Printer data=printerDAO.query(id);
		if(data==null){
			throw new PrinterNotFoundException("打印机未找到");
		}
		
		PrintMessageUtils.shutdownRestartRequest(data.getDeviceNumber(), ResponseType.SHUTDOWN.toStringValue());
		return result;
	}
	
	@Override
	public ResultVO restart(String id){
		ResultVO result=new ResultVO();
		
		Printer data=printerDAO.query(id);
		if(data==null){
			throw new PrinterNotFoundException("打印机未找到");
		}
		
		PrintMessageUtils.shutdownRestartRequest(data.getDeviceNumber(), ResponseType.RESTART.toStringValue());
		
		return result;
	}
	
	
	public ResultVO setSound(@PathVariable String id,PrinterVoiceVO printerVoiceVO){
		ResultVO result=new ResultVO();
		
		Printer data=printerDAO.query(id);
		if(data==null){
			throw new PrinterNotFoundException("打印机未找到");
		}
		
		if(printerVoiceVO.getSoundType()==Is.NO.toIntValue()){
			PrintMessageUtils.setSoundRequest(data.getDeviceNumber(), ResponseType.HORN.toStringValue(), printerVoiceVO.getSound());
		}else if(printerVoiceVO.getSoundType()==Is.YES.toIntValue()){
			PrintMessageUtils.setSoundRequest(data.getDeviceNumber(), ResponseType.BUZZER.toStringValue(), printerVoiceVO.getSound());
		}
		
		Printer printer=new Printer();
		printer.setId(id);
		printer.setSound(printerVoiceVO.getSound());
		printer.setSoundType(printerVoiceVO.getSoundType());
		printerDAO.update(printer);
		
		return result;
	}
	
	@Override
	public ResultVO setVoice(String id,PrinterVoiceVO printerVoiceVO){
		ResultVO result=new ResultVO();
		
		Printer data=printerDAO.query(id);
		if(data==null){
			throw new PrinterNotFoundException("打印机未找到");
		}
		
		if(printerVoiceVO.getIsOpenVoice()==Is.YES.toIntValue()){
			String content="[\""+Constants.RECOMMAND_VOICE+"\","+printerVoiceVO.getVoice()+",0]";
			PrintMessageUtils.setVoiceRequest(data.getDeviceNumber(),content, false,Constants.ZERO);
		}else if(printerVoiceVO.getIsOpenVoice()==Is.NO.toIntValue()){
			PrintMessageUtils.cancelVoiceRequest(data.getDeviceNumber(),Constants.ZERO);
		}
		
		Printer printer=new Printer();
		printer.setId(id);
		printer.setIsOpenVoice(printerVoiceVO.getIsOpenVoice());
		printer.setVoice(printerVoiceVO.getVoice());
		printerDAO.update(printer);
		
		return result;
		
	}
	
}
